<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Calendar" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>${movie.title} - 选择场次</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .movie-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 2rem 0;
        }
        .movie-poster {
            width: 150px;
            height: 200px;
            object-fit: cover;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }
        .movie-info h2 {
            margin-bottom: 1rem;
        }
        .movie-meta {
            margin-bottom: 0.5rem;
            opacity: 0.9;
        }
        .showtime-card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            margin-bottom: 1.5rem;
            overflow: hidden;
        }
        .cinema-header {
            background: #f8f9fa;
            padding: 1rem 1.5rem;
            border-bottom: 1px solid #dee2e6;
        }
        .cinema-name {
            font-size: 1.2rem;
            font-weight: bold;
            color: #333;
            margin-bottom: 0.5rem;
        }
        .cinema-address {
            color: #666;
            font-size: 0.9rem;
        }
        .showtime-list {
            padding: 1.5rem;
        }
        .showtime-item {
            display: inline-block;
            margin: 0.5rem;
            padding: 1rem 1.5rem;
            border: 2px solid #e9ecef;
            border-radius: 10px;
            text-decoration: none;
            color: #333;
            transition: all 0.3s ease;
            min-width: 200px;
            text-align: center;
        }
        .showtime-item:hover {
            border-color: #667eea;
            color: #667eea;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.2);
        }
        .showtime-time {
            font-size: 1.2rem;
            font-weight: bold;
            margin-bottom: 0.5rem;
        }
        .showtime-price {
            color: #dc3545;
            font-weight: bold;
            font-size: 1.1rem;
            margin-bottom: 0.5rem;
        }
        .showtime-seats {
            color: #666;
            font-size: 0.9rem;
        }
        .showtime-hall {
            color: #666;
            font-size: 0.9rem;
            margin-bottom: 0.5rem;
        }
        .sold-out {
            background: #f8f9fa;
            color: #6c757d;
            border-color: #dee2e6;
            cursor: not-allowed;
        }
        .sold-out:hover {
            background: #f8f9fa;
            color: #6c757d;
            border-color: #dee2e6;
            transform: none;
            box-shadow: none;
        }
        .expired {
            background: #f8f9fa;
            color: #6c757d;
            border-color: #dee2e6;
            cursor: not-allowed;
            opacity: 0.6;
        }
        .expired:hover {
            background: #f8f9fa;
            color: #6c757d;
            border-color: #dee2e6;
            transform: none;
            box-shadow: none;
        }
        .expired .showtime-seats {
            color: #dc3545;
        }
        .no-showtimes {
            text-align: center;
            padding: 3rem;
            color: #666;
        }
    </style>
</head>
<body class="bg-light">
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
        <div class="container">
            <a class="navbar-brand fw-bold text-primary" href="${pageContext.request.contextPath}/movies">
                <i class="fas fa-film me-2"></i>影院订票系统
            </a>

            <div class="navbar-nav ms-auto">
                <div class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">
                        <i class="fas fa-user me-1"></i>${sessionScope.user.username}
                    </a>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/user/orders">
                            <i class="fas fa-ticket-alt me-2"></i>我的订单
                        </a></li>
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/user/profile">
                            <i class="fas fa-user-edit me-2"></i>个人信息
                        </a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/logout">
                            <i class="fas fa-sign-out-alt me-2"></i>退出登录
                        </a></li>
                    </ul>
                </div>
            </div>
        </div>
    </nav>

    <!-- 电影信息头部 -->
    <section class="movie-header">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-md-3">
                    <img src="${movie.posterUrl != null ? movie.posterUrl : '/images/default-poster.jpg'}"
                         class="movie-poster" alt="${movie.title}">
                </div>
                <div class="col-md-9">
                    <div class="movie-info">
                        <h2>${movie.title}</h2>
                        <c:if test="${not empty movie.englishTitle}">
                            <div class="movie-meta">
                                <i class="fas fa-globe me-1"></i>${movie.englishTitle}
                            </div>
                        </c:if>
                        <div class="movie-meta">
                            <i class="fas fa-user-tie me-1"></i>导演：${movie.director}
                        </div>
                        <c:if test="${not empty movie.actors}">
                            <div class="movie-meta">
                                <i class="fas fa-users me-1"></i>主演：${movie.actors}
                            </div>
                        </c:if>
                        <div class="movie-meta">
                            <i class="fas fa-tags me-1"></i>类型：${movie.genre}
                        </div>
                        <div class="movie-meta">
                            <i class="fas fa-clock me-1"></i>时长：${movie.duration}分钟
                        </div>
                        <c:if test="${not empty movie.rating}">
                            <div class="movie-meta">
                                <i class="fas fa-star me-1"></i>评分：${movie.rating}
                            </div>
                        </c:if>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <div class="container mt-4">
        <!-- 返回按钮 -->
        <div class="mb-3">
            <a href="${pageContext.request.contextPath}/movies" class="btn btn-outline-primary">
                <i class="fas fa-arrow-left me-1"></i>返回电影列表
            </a>
        </div>

        <!-- 场次列表 -->
        <c:if test="${empty showtimes}">
            <div class="no-showtimes">
                <i class="fas fa-calendar-times fa-3x mb-3 text-muted"></i>
                <h4>暂无场次安排</h4>
                <p class="text-muted">该电影暂时没有可预订的场次，请稍后再试</p>
            </div>
        </c:if>

        <c:if test="${not empty showtimes}">
            <!-- 按影院分组显示场次 -->
            <c:set var="currentCinemaId" value="" />
            <c:forEach var="showtime" items="${showtimes}">
                <c:if test="${showtime.cinemaId != currentCinemaId}">
                    <c:if test="${not empty currentCinemaId}">
                        </div></div> <!-- 关闭上一个影院的div -->
                    </c:if>
                    <div class="showtime-card">
                        <div class="cinema-header">
                            <div class="cinema-name">${showtime.cinema.name}</div>
                            <div class="cinema-address">
                                <i class="fas fa-map-marker-alt me-1"></i>${showtime.cinema.address}
                            </div>
                        </div>
                        <div class="showtime-list">
                    <c:set var="currentCinemaId" value="${showtime.cinemaId}" />
                </c:if>

                <!-- 场次项 -->
                <%
                    // 获取当前时间
                    Date now = new Date();
                    Calendar nowCal = Calendar.getInstance();
                    nowCal.setTime(now);

                    // 获取场次时间
                    Object showtimeObj = pageContext.getAttribute("showtime");
                    if (showtimeObj != null) {
                        com.cinema.entity.Showtime showtime = (com.cinema.entity.Showtime) showtimeObj;

                        // 组合场次的日期和时间
                        Calendar showtimeCal = Calendar.getInstance();
                        showtimeCal.setTime(showtime.getShowDate());

                        Calendar timeCal = Calendar.getInstance();
                        timeCal.setTime(showtime.getShowTime());

                        showtimeCal.set(Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY));
                        showtimeCal.set(Calendar.MINUTE, timeCal.get(Calendar.MINUTE));
                        showtimeCal.set(Calendar.SECOND, 0);

                        Date showtimeDateTime = showtimeCal.getTime();
                        boolean isExpired = showtimeDateTime.before(now);

                        pageContext.setAttribute("isExpired", isExpired);
                    }
                %>

                <c:choose>
                    <c:when test="${isExpired}">
                        <!-- 已过期的场次 -->
                        <div class="showtime-item expired">
                            <div class="showtime-time">
                                <fmt:formatDate value="${showtime.showTime}" pattern="HH:mm"/>
                            </div>
                            <div class="showtime-hall">${showtime.hall.name}</div>
                            <div class="showtime-price">¥${showtime.price}</div>
                            <div class="showtime-seats">已过期</div>
                        </div>
                    </c:when>
                    <c:when test="${showtime.availableSeats > 0}">
                        <!-- 有票的场次 -->
                        <div class="showtime-item" onclick="bookShowtime(${showtime.id})" style="cursor: pointer;">
                            <div class="showtime-time">
                                <fmt:formatDate value="${showtime.showTime}" pattern="HH:mm"/>
                            </div>
                            <div class="showtime-hall">${showtime.hall.name}</div>
                            <div class="showtime-price">¥${showtime.price}</div>
                            <div class="showtime-seats">
                                余票${showtime.availableSeats}张
                            </div>
                        </div>
                    </c:when>
                    <c:otherwise>
                        <!-- 已售罄的场次 -->
                        <div class="showtime-item sold-out">
                            <div class="showtime-time">
                                <fmt:formatDate value="${showtime.showTime}" pattern="HH:mm"/>
                            </div>
                            <div class="showtime-hall">${showtime.hall.name}</div>
                            <div class="showtime-price">¥${showtime.price}</div>
                            <div class="showtime-seats">已售罄</div>
                        </div>
                    </c:otherwise>
                </c:choose>
            </c:forEach>

            <c:if test="${not empty currentCinemaId}">
                </div></div> <!-- 关闭最后一个影院的div -->
            </c:if>
        </c:if>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function bookShowtime(showtimeId) {
            <c:if test="${empty sessionScope.user}">
                alert('请先登录后再订票');
                window.location.href = '${pageContext.request.contextPath}/login';
                return;
            </c:if>

            window.location.href = '${pageContext.request.contextPath}/user/booking?showtimeId=' + showtimeId;
        }
    </script>
</body>
</html>
